package com.daos;

import com.exceptions.JobNotFoundException;
import com.models.Job;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

@Repository
public class JobDao {

  @Autowired
  private MongoTemplate mongo;

  public Job getJob(String id) throws JobNotFoundException {
    Query query = new Query();
    query.addCriteria(Criteria.where("_id").is(id));
    Job job = mongo.findOne(query, Job.class);

    if (job == null) {
      throw new JobNotFoundException("Job not found.");
    }

    return job;
  }

  public void saveJob(Job job) {
    mongo.save(job);
  }

  public String getFileNameFor(String file_id) {
    Query query = new Query();
    query.addCriteria(Criteria.where("_id").is(file_id));
    Job job = mongo.findOne(query, Job.class);

    return (String) job.getJobResult();
  }

}
